use "$clean/prowess_wits_IO.dta", clear
******************************************************************************
********************************** Data Cleaning *****************************
******************************************************************************
rename products_cocode1 co_code1
bysort co_code1 prod_date3: generate nmbr_prods = _N
generate date_string = prod_date3 
generate year2  = substr(date_string,1,4)
generate month2  = substr(date_string,5,2)
generate day2  = substr(date_string,7,2)

keep if ann_rep_months4==12 // Keep firms reporting data for 12 months.
keep if month2=="03" 

destring year, replace

merge m:1 co_code1 year using "$working/perpetual_inventory_capital"
drop if _merge==2
drop _merge

merge m:1 year using "$working/wholesale_priceindex"
drop if _merge==2
drop _merge

*Instead of 100, make base year 1
replace wholesale_priceindex = wholesale_priceindex/100

sum  wholesale_priceindex, detail

generate wL = sa_compensation_to_e_160/wholesale_priceindex
generate pM = sa_rawmat_stores_spa_146/wholesale_priceindex
generate pE = energy_cons_value9/wholesale_priceindex

foreach variable in wL rK pM pE {
	drop if `variable'==.
}


keep if nic_08_2dig =="10" | nic_08_2dig =="13" | nic_08_2dig =="17" | nic_08_2dig =="20" | nic_08_2dig =="21"  | nic_08_2dig =="22" | nic_08_2dig =="23" ///
 | nic_08_2dig =="24" | nic_08_2dig =="25" | nic_08_2dig =="26"| nic_08_2dig =="27" | nic_08_2dig =="28" | nic_08_2dig =="29"

generate TC = wL + rK + pM + pE


**Make observation a single firm
bysort co_code1 year: generate count_thing = _n
keep if count_thing==1

foreach variable in wL rK pM pE {
	sum `variable', detail
	local p1 = r(p1)
	local p99 = r(p99)
	drop if `variable' < `p1'
	drop if `variable' > `p99'
	
}

merge 1:m year co_code1 using "$tempfold/bssample2"
keep if _merge==3
drop _merge	

preserve
	generate beta_obs = 1
	collapse (sum) wL rK pM pE TC beta_obs
	generate beta_l_hat_q = wL/TC
	generate beta_k_hat_q = rK/TC
	generate beta_m_hat_q = pM/TC
	generate beta_e_hat_q = pE/TC
	
	sum beta_*, detail

	save "$tempfold/CS_klme_OPF", replace
	
restore


preserve
	generate beta_obs = 1
	collapse (sum) wL rK pM pE TC beta_obs, by(nic_08_2dig)
	generate beta_l_hat_q = wL/TC
	generate beta_k_hat_q = rK/TC
	generate beta_m_hat_q = pM/TC
	generate beta_e_hat_q = pE/TC
	
	sum beta_*, detail

	save "$tempfold/CS_klme_nic2", replace

restore


preserve
	generate beta_obs = 1
	collapse (sum) wL rK pM pE TC beta_obs, by(nic_08_4dig)
	generate beta_l_hat_q = wL/TC
	generate beta_k_hat_q = rK/TC
	generate beta_m_hat_q = pM/TC
	generate beta_e_hat_q = pE/TC
	
	sum beta_*, detail

	save "$tempfold/CS_klme_nic4", replace
	
restore
